package com.kai.controller;

import com.kai.pojo.Coach;
import com.kai.pojo.Course;
import com.kai.pojo.CourseType;
import com.kai.service.CourseService;
import com.kai.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 课程
 */
@Controller
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 查询教练和课程类型
     * @return
     */
    @GetMapping("/courseTypeAndCoach")
    @ResponseBody
    public Map courseTypeAndCoach(){
        List<CourseType> types = courseService.selectAllType();
        List<Coach> coaches = courseService.selectCoach();
        Map<String, Object> map = new HashMap<>();
        map.put("types",types);
        map.put("coaches",coaches);
        return map;
    }


    /**
     * 修改或添加课程
     * @param course
     * @return
     */
    @PostMapping("/updateAndAddCourse")
    @ResponseBody
    public String updateAndAddCourse(@RequestBody Course course){
        if (!course.getId().equals("")){
            courseService.updateCourse(course,course.getCourseType().getId(),course.getCoach().getId());
            return "updateCourse";
        }else {
            if (courseService.selectCourseByName(course.getName())<1){
            course.setCreadedate(new Date());
            course.setId(UUIDUtils.getUUID());
            courseService.addCourse(course,course.getCourseType().getId(),course.getCoach().getId());
            return "addCourse";
            }
            return "courseExist";
        }
    }
    /**
     * 模糊查询课程
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectAllCourse")
    @ResponseBody
    public Map<String, Object> selectAllCourse(String name,int pageNum, int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        int pageTotal = courseService.selectAllCourseCount(name);
        List<Course> courseList = courseService.selectAllCourse(name,(pageNum-1)*pageSize,pageSize);
        map.put("courseList",courseList);
        map.put("pageTotal",pageTotal);
        return map;
    }

    /**
     * 删除课程
     * @param ids
     * @return
     */
    @GetMapping("/deleteCourse")
    @ResponseBody
    public int deleteCourse(String[] ids){
        int i = 0;
        List<String> idList = Stream.of(ids).distinct().collect(Collectors.toList());
        for (String id : idList) {
            i = courseService.deleteCourse(id);
            i++;
        }
        return i;
    }

    /**
     *查询全部课程
     * @return
     */
    @GetMapping("/selectCourse")
    @ResponseBody
    public List<Course> selectCourse(String coach_id,String courseType_id){
        List<Course> courses = courseService.selectCourse(coach_id,courseType_id);
        return courses;
    }

    /**
     * 根据id来查询课程
     * @param id
     * @return
     */
    @GetMapping("/selectCourseById")
    @ResponseBody
    public Course  selectCourseById(String id){
        return courseService.selectCourseById(id);
    }

    /*查询课程类型*/
    @GetMapping("/selectCourseType")
    @ResponseBody
    public List<CourseType> selectCourseType(){
        return courseService.selectAllType();
    }
}
